#-----------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------#

### Accountability and Corruption Displacement: Evidence from Italy
### Eleanor Florence Woodhouse

#-----------------------------------------------------------------------------------------------#

rm(list=ls(all=TRUE))
options(scipen = 999,
        sf_max.plot=1,
        warn=-1)

#install.packages("tidyverse")
#install.packages("broom")
#install.packages("sf")
#install.packages("stargazer")

library(tidyverse)
library(broom)
library(sf)
library(stargazer)

#-----------------------------------------------------------------------------------------------#

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))
load("replication_files_JPP.RData")

#-----------------------------------------------------------------------------------------------#

# TABLE 1 - DESCRIPTIVE STATISTICS
stargazer(data[,c("district_id","year","treated","post","peculato_people","length_judicial_process","macro_region_num")],
          covariate.labels=c("Electoral District",
            "Year",
            "Treated",
            "Post",
            "Corruption Indictments",
            "Length of Judicial Proceedings",
            "Macro Regions of Italy"),
          omit.summary.stat = c("p25", "p75"),
          type="text")

#-----------------------------------------------------------------------------------------------#

#DID - INDIVIDUAL
data <- data %>% 
  dplyr::mutate(did=post*treated,
                post = as.numeric(post))

#REGRESSIONS
m1 <- lm(peculato_people ~ treated + post + did + factor(district_id), data = data)
m2 <- lm(peculato_people ~ treated + post + did + year + factor(district_id), data = data)
m3 <- lm(peculato_people ~ treated + post + did + year + length_judicial_process + factor(district_id), data = data)
m4 <- lm(peculato_people ~ treated + post + did + year + length_judicial_process + factor(macro_region_num) + factor(district_id), data = data)

#PRINTING REGRESSION TABLE
stargazer(m1, m2, m3, m4,
          dep.var.labels = "Corruption Indictments",
          covariate.labels=c("Treated",
                             "Post",
                             "Treated*Post",
                             "Year",
                             "Length of Judicial Proceedings",
                             "Macro Regions: Centre",
                             "Macro Regions: South",
                             "Macro Regions: Islands"),
          omit.stat=c("f"),
          omit=c("factor\\(district_id\\).*"),
          add.lines=list(c('District FE', 'YES', 'YES', 'YES', 'YES')),
          type='text')

#-----------------------------------------------------------------------------------------------#

##ROBUSTNESS TESTS - MAIN TEXT

#LAGGING CORRUPTION INDICTMENTS 
lag_data <- data %>% 
  group_by(district_id) %>%
  dplyr::mutate(lag_peculato_people=lag(peculato_people),
                lag2_peculato_people=lag(lag(peculato_people)))

lag1 <- lm(lag_peculato_people ~ treated + post + did + year + length_judicial_process + factor(macro_region_num) + factor(district_id), data = lag_data)
lag2 <- lm(lag2_peculato_people ~ treated + post + did + year + length_judicial_process + factor(macro_region_num) + factor(district_id), data = lag_data)

#SENSITIVITY ANALYSES
sens1 <- lm(peculato_crimes ~ treated + post + did + year + length_judicial_process + factor(macro_region_num) + factor(district_id), data = data)
sens2 <- lm(omissione_people ~ treated + post + did + year + length_judicial_process + factor(macro_region_num) + factor(district_id), data = data)
sens3 <- lm(violenza_people ~ treated + post + did + year + length_judicial_process + factor(macro_region_num) + factor(district_id), data = data)

stargazer(lag1, lag2, sens1, sens2, sens3,
          dep.var.labels.include = FALSE,
          column.labels=c('Corruption Lag1','Corruption Lag2',"Corruption Charges","Omission","Violence"),
          omit.stat=c("f"),
          omit=c("factor.*","year","length_judicial_process"),
          covariate.labels=c("Treated",
                             "Post",
                             "Treated*Post"),
          add.lines=list(c('Full controls', 'YES','YES',"YES","YES","YES"),
                         c('District FE', 'YES','YES',"YES","YES","YES")),
          type="text")

#-----------------------------------------------------------------------------------------------#

# FIGURE 2 - PARALLEL TRENDS

data_regression <- data %>% 
  group_by(year,treated) %>% 
  dplyr::summarise(across(c(peculato_people), ~ sum(.x, na.rm = T))) %>% 
  dplyr::mutate(treatment=as.factor(ifelse(treated==1,"Treatment","Control")))

data_regression %>% 
  ggplot(aes(x=year, y=peculato_people, group=treatment, linetype = treatment)) +
  geom_vline(xintercept = 1992, size=1, color="grey20",alpha = 0.5) +
  geom_line(size = 1,alpha = 0.9) + 
  scale_linetype_manual(breaks=c("Treatment","Control"), values=c(1,3)) +
  ggtitle(label = "CORRUPTION INDICTMENTS",
          subtitle = "Italy, 1982-1999") +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 15, face = "bold"),
    plot.subtitle = element_text(hjust = 0.5, size = 12),
    legend.position = "bottom",
    legend.title = element_blank(),
    axis.title.y = element_blank(),
    axis.title.x = element_blank(),
    axis.ticks.y=element_blank(),
    panel.grid.minor = element_blank())

#-----------------------------------------------------------------------------------------------#

# FIGURE 3 - MAP OF TREATED AND CONTROL DISTRICTS

#DISTRICTS
districts <- map %>% 
  group_by(COD_DIST,delta) %>% 
  summarise(across(c(treated), mean))

#CENTROIDS
districts_cen = st_point_on_surface(districts) %>% 
  mutate(cex=5*(delta+abs(min(delta))+0.01))

districts_1 <- districts %>% filter(treated==1)
districts_0 <- districts %>% filter(treated==0)

plot(districts_1["treated"], border = 'grey30', lwd = 1, 
     key.pos = 1,key.width = lcm(1.3), key.length = 1.0,
     main = "",
     breaks = "quantile", nbreaks = 7,
     col="grey50",
     reset=F)
plot(districts_0["treated"], border = 'grey30', lwd = 1, 
     key.pos = 1,key.width = lcm(1.3), key.length = 1.0,
     col="grey70",
     add=T)
plot(districts_cen, add=T,col="black",cex=districts_cen$cex)


#-----------------------------------------------------------------------------------------------#

#FIGURE 4 - LAG YEAR INTERACTIONS TO TEST FOR PRE-TREATMENT EFFECTS

data <- data %>% 
  dplyr::mutate(year_f=as.factor(year),
                year_f = relevel(year_f,"1992"))

fit <- lm(peculato_people ~ treated*year_f + length_judicial_process + factor(macro_region_num) + factor(district_id), data = data) 

fit_cis_95 <- confint(fit, level = 0.95) %>% 
  data.frame() %>%
  dplyr::rename("conf.low_95" = "X2.5..","conf.high_95" = "X97.5..")
fit_cis_90 <- confint(fit, level = 0.90) %>% 
  data.frame() %>%
  dplyr::rename("conf.low_90" = "X5..","conf.high_90" = "X95..")

results <- broom::tidy(fit)
results <- bind_cols(results, fit_cis_95, fit_cis_90) %>%
  dplyr::rename(Variable = term,
         Coefficient = estimate,
         SE = std.error) %>%
  dplyr::filter(grepl(":",Variable)) %>% 
  dplyr::filter(!grepl("93$|94$|95$|96$|97$|98$|99$",Variable)) 

ggplot(results, aes(x = Variable, y = Coefficient)) +
  geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) +
  geom_point(aes(x = Variable, y = Coefficient)) + 
  geom_linerange(aes(x = Variable, ymin = conf.low_90, ymax = conf.high_90), lwd = 1) +
  geom_linerange(aes(x = Variable, ymin = conf.low_95, ymax = conf.high_95), lwd = 1/2) + 
  scale_x_discrete(labels = c('Treated*1991','Treated*1990','Treated*1989','Treated*1988','Treated*1987','Treated*1986','Treated*1985','Treated*1984','Treated*1983','Treated*1982')) +
  coord_flip() +
  xlab(NULL) +
  theme_minimal()


#-----------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------#
#-----------------------------------------------------------------------------------------------#
